package com.longtop.leaning.dao;

import java.util.Date;
import java.util.Enumeration;
import java.util.Vector;

import javax.microedition.rms.RecordComparator;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordFilter;
import javax.microedition.rms.RecordStore;

import com.longtop.leaning.entity.FlightData;
import com.longtop.leaning.util.Base64;
import com.longtop.leaning.util.StringUtils;

public class FltRMSDao implements FltDao{

	private static final FltRMSDao instance = new FltRMSDao();

	private FltRMSDao() {
	}

	public static FltRMSDao getInstance() {
		return instance;
	}
	
	public void test() {
		try {
			RecordStore rs = RMSUtil.getFltRecordStore();

			FlightData flt = new FlightData();
			flt.setId(rs.getNextRecordID());
			flt.setFltdate(new Date());
			flt.setFltno("测FM"+rs.getNextRecordID()+"-中国");

			//System.out.println(rs.getSize());
			System.out.println("getNumRecords:"+rs.getNumRecords());

			/*RecordEnumeration re = rs.enumerateRecords(new FltFilter("", "", -1), new FltRecordComparator(), false);

			while (re.hasNextElement()) {
				byte nextRec[] = re.nextRecord();
				System.out.println("read record bytes : " + nextRec);
				flt = (VFlt) FltSerializable.getInstance().deserialize(nextRec);
				System.out.println("主键ID：" + flt.getId());
				System.out.println(", 航班号：" + flt.getFltNum());
			}

			Vector flts = findFlts("3", "", -1);
			System.out.println("flts size: " + flts.size());*/
			//addFlts(flts);
			//System.out.println("after add flts: "+rs.getNumRecords());
			RMSUtil.closeRecordStore(rs);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void addFlts(Vector strings) {
		RecordStore rs = RMSUtil.getFltRecordStore();
		
		Enumeration fltE = strings.elements();
		while(fltE.hasMoreElements()){
			String flt = (String) fltE.nextElement();
			System.out.println("rms store flight: " + flt);
			RMSUtil.addRecord(rs, Base64.decode(flt));
		}
		
		RMSUtil.closeRecordStore(rs);
	}

	public Vector findFlts(String fltno, Date fltdateStart, Date fltdateEnd, int fltstatus) {
		RecordStore rs = RMSUtil.getFltRecordStore();
		RecordEnumeration re = RMSUtil.enumerateRecords(rs, new FltFilter(fltno, fltdateStart, fltdateEnd, fltstatus),
				new FltRecordComparator(), false);
		Vector flts = new Vector();
		while (re.hasNextElement()) {
			byte nextRec[] = RMSUtil.nextRecord(re);
			flts.addElement(FlightData.deserialize(nextRec));
		}
		
		System.out.println("rms get flights: get "+ flts.size());
		RMSUtil.closeRecordStore(rs);
		return flts;
	}
	
	public void clearFlt() {
		RMSUtil.deleteRecordStore(RMSUtil.FLT_RMS_NAME);
	}

	class FltFilter implements RecordFilter {
		private String fltno = ""; // 航班号
		private Date fltdateStart = null;// 计划起飞日期及时间
		private Date fltdateEnd = null;// 计划起飞日期及时间
		private int fltstatus = -1;// 航班起飞状态 0 正常 1 延误 2 取消

		public FltFilter(String fltno, Date fltdateStart, Date fltdateEnd, int fltstatus) {
			this.fltno = fltno;
			this.fltdateStart = fltdateStart;
			this.fltdateEnd = fltdateEnd;
			this.fltstatus = fltstatus;
		}

		public boolean matches(byte[] candidate) {
			FlightData flt = (FlightData) FlightData.deserialize(candidate);

			if (!StringUtils.isEmpty(fltno)) {
				return flt.getFltno().indexOf(fltno.trim()) != -1;
			}

			if (fltdateStart != null) {
				return flt.getFltdate().getTime() > fltdateStart.getTime();
			}
			
			if (fltdateEnd != null) {
				return flt.getFltdate().getTime() < fltdateEnd.getTime();
			}

			if (fltstatus >= 0) {
				return flt.getFltstatus() == fltstatus;
			}

			return true;
		}
	}

	class FltRecordComparator implements RecordComparator {
		public int compare(byte[] rec1, byte[] rec2) {
			FlightData flt1 = (FlightData) FlightData.deserialize(rec1);
			FlightData flt2 = (FlightData) FlightData.deserialize(rec2);

			if (flt1.getId() < flt2.getId()) {
				return RecordComparator.PRECEDES;
			} else if (flt1.getId() > flt2.getId()) {
				return RecordComparator.FOLLOWS;
			}
			return RecordComparator.EQUIVALENT;
		}
	}

}
